package com.tmc.util;

import java.util.Date;
import java.util.List;
import java.util.Map;
import com.tmc.bean.common.CommonResult;
import com.tmc.bean.common.ContextInfo;
import com.tmc.bean.intair.EthFare;
import com.tmc.bean.intair.EthShoppingReq;
import com.tmc.constant.CacheConstant;
import com.tmc.service.cache.CacheServiceImpl;

public class IntairUtil {

	public static EthShoppingReq createNewShoppingReq(){
		EthShoppingReq req = new EthShoppingReq();
		req.setFlightType("RT");
		req.setDeparture("PVG");
		req.setArrival("HKG");
		req.setDepartureDate(DateUtil.toStrDate(new Date(), DateUtil.yyyy_MM_dd));
		req.setReturnDate(DateUtil.toStrDate(DateUtil.addDate(new Date(), 2), DateUtil.yyyy_MM_dd));
		return req;
	}
	
	public static String getCacheKey(ContextInfo context, String reqJSON) {
		return "KEY_SHOPPING_" + reqJSON;
	}
	
	@SuppressWarnings("unchecked")
	public static CommonResult getFareFromCache(String fareKey, CacheServiceImpl cache){
		Map<String, String> map = (Map<String, String>) cache.get(CacheConstant.KEY_SHOPPING_FAREKEY);
		if (map == null || map.isEmpty()) {
			return CommonResult.makeFail("璇烽噸鏂版煡璇紒102");
		}
		String reqJson = map.get(fareKey);
		List<EthFare> fares = (List<EthFare>) cache.get(getCacheKey(null, reqJson));
		if (fares == null || fares.isEmpty()) {
			return CommonResult.makeFail("瓒呮椂锛岃閲嶆柊鏌ヨ锛�103");
		}
		EthFare fare = getFare(fares, fareKey);
		if (fare == null) {
			return CommonResult.makeFail("瓒呮椂锛岃閲嶆柊鏌ヨ锛�104");
		}
		return CommonResult.makeSuccess("", fare);
	}
	
	public static EthFare getFare(List<EthFare> fares, String fareKey) {
		for (EthFare fare : fares) {
			if (fareKey.equals(fare.getFareKey())) {
				return fare;
			}
		}
		return null;
	}
}
